package com.xxd.utils;

import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;


public class EHCacheUtils {

	private static CacheManager cacheManager = CacheManager.newInstance();
	private static final String TOKEN_CACHE_AREA = "tokenRetryCache";

	/**
	 * 设置缓存对象
	 * 
	 * @param key
	 * @param object
	 */
	public static void setTokenCache(String key, Object object) {
		Cache cache = cacheManager.getCache(TOKEN_CACHE_AREA);
		Element element = new Element(key, object);
		cache.put(element);
	}

	/**
	 * 从缓存中取出对象
	 *
	 * @param key
	 * @return
	 */
	public static Object getTokenCache(String key) {
		Object object = null;
		Cache cache = cacheManager.getCache(TOKEN_CACHE_AREA);
		if (cache.get(key) != null && !cache.get(key).equals("")) {
			object = cache.get(key).getObjectValue();
		}
		return object;
	}

	public static boolean delTokenCache(Object key) {
		Cache cache = cacheManager.getCache(TOKEN_CACHE_AREA);
		return cache.remove(key);
	}

	public static  void clearTokenCache(){
		cacheManager.clearAll();
	}
}
